package com.knowledge.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;

import com.knowledge.common.annotation.Log;
import com.knowledge.common.core.controller.BaseController;
import com.knowledge.common.core.domain.AjaxResult;
import com.knowledge.common.enums.BusinessType;
import com.knowledge.common.utils.SecurityUtils;
import com.knowledge.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.knowledge.business.domain.KnowledgeType;
import com.knowledge.business.service.IKnowledgeTypeService;
import com.knowledge.common.core.page.TableDataInfo;

/**
 * 知识库分类Controller
 *
 * @author zhaoyk
 * @date 2025-09-13
 */
@RestController
@RequestMapping("/business/type")
public class KnowledgeTypeController extends BaseController {
    @Autowired
    private IKnowledgeTypeService knowledgeTypeService;

    /**
     * 查询知识库分类列表
     */
//@RequiresPermissions("business:type:list")
    @GetMapping("/list")
    public TableDataInfo list(KnowledgeType knowledgeType) {
        startPage();
        List<KnowledgeType> list = knowledgeTypeService.selectKnowledgeTypeList(knowledgeType);
        return getDataTable(list);
    }

    @GetMapping("/alllist")
    public AjaxResult alllist(KnowledgeType knowledgeType) {
        List<KnowledgeType> list = knowledgeTypeService.selectKnowledgeTypeList(knowledgeType);
        return AjaxResult.success(list);
    }

    /**
     * 导出知识库分类列表
     */
    //@RequiresPermissions("business:type:export")
    @Log(title = "知识库分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KnowledgeType knowledgeType) {
        List<KnowledgeType> list = knowledgeTypeService.selectKnowledgeTypeList(knowledgeType);
        ExcelUtil<KnowledgeType> util = new ExcelUtil<KnowledgeType>(KnowledgeType.class);
        util.exportExcel(response, list, "知识库分类数据");
    }

    /**
     * 获取知识库分类详细信息
     */
    //@RequiresPermissions("business:type:query")
    @GetMapping(value = "/view/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(knowledgeTypeService.getById(id));
    }

    /**
     * 新增知识库分类
     */
    //@RequiresPermissions("business:type:add")
    @Log(title = "知识库分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody KnowledgeType knowledgeType) {
        knowledgeType.setUserId(SecurityUtils.getUserIdStr());
        return toAjax(knowledgeTypeService.save(knowledgeType));
    }

    /**
     * 修改知识库分类
     */
    //@RequiresPermissions("business:type:edit")
    @Log(title = "知识库分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowledgeType knowledgeType) {
        return toAjax(knowledgeTypeService.updateById(knowledgeType));
    }

    /**
     * 删除知识库分类
     */
    //@RequiresPermissions("business:type:remove")
    @Log(title = "知识库分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        knowledgeTypeService.deleteSj(ids);
        return AjaxResult.success();
    }
}
